/**
 * Created by Administrator on 2015/12/3.
 */
"use strict";
var F = require('../../../common/function');
var order_F = require('./order_common');
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var _ = require('underscore');

module.exports.coupon_list = function*(queryObj){
    //根据订单id查找瓷肌券，结果中根据机构id,查询机构详情
    queryObj.id = _.v.isInt(queryObj.id)? parseInt(queryObj.id):queryObj.id.split(',') ;
    queryObj.act =1;
    let couponResult = yield o2oServiceApi.post('order','getTicket',1,queryObj);
    if(couponResult.status ==400 || couponResult.result['error'] ==1){
        return F.returnMsg(400,'查找失败！',1);
    }else{
        if(couponResult['result']['data'] && couponResult['result']['data'].length == 0){
            return F.returnMsg(300, '已经没有数据', 1, couponResult['result']['data']);
        }
    }
    //console.log(couponResult);
    //获取机构信息
    let shopInfo =[];
    let orderResult = yield  o2oServiceApi.post('order','detail',1,{"act":queryObj.act,"orderid":queryObj.id});
    if(orderResult.status == 200  ||  orderResult['result']['ShopInfo'].length>0){
        shopInfo =orderResult['result']['ShopInfo'];
    }
    //console.log(shopInfo);
    let ticketInfo =[];
    for(let item of couponResult['result']['data']){
        let couponObj ={};
        couponObj.shopName ='';
        couponObj.ticket_id =item.ticket_id;
        couponObj.ticket_code =item.ticket_code;
        couponObj.money =item.money;
        couponObj.order_id = item.order_id;
        couponObj.shopid = item.shopid;
        couponObj.status = item.status;
        couponObj.appraise_state =item.appraise_state;
        couponObj.consume_date =item.consume_date;
        couponObj.valid_start_date =item.valid_start_date;
        couponObj.valid_end_date =item.valid_end_date;
        couponObj.cancel_status =item.cancel_status;
        couponObj.apply_cancel_date =item.apply_cancel_date;
        couponObj.app_cancel_date =item.app_cancel_date;
        couponObj.alipay_date =item.alipay_date;
        couponObj.payback_date = item.payback_date;
        couponObj.payback_option =item.payback_option;

        if(couponObj.shopid>0 && shopInfo.length>0){
            for(let i of shopInfo){
                if(i.id == couponObj.shopid){
                    couponObj.shopName = i.shopName;
                    break;
                }
            }
        }
        ticketInfo.push(couponObj);
    }
    return F.returnMsg(200, '查找成功', 1, ticketInfo);
}